<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<script type="text/javascript" src="${path}/wcfsy-wechat/js/jquery-1.7.2.min.js"></script>
<script>
var appId;
var timeStamp;
var nonceStr;
var Package;
var signType;
var paySign;
var orderNumber;
$(function(){
	$.ajax({
		type : "post",
		dataType : 'json',
		async: false,
		url : '/wcfsy-wechat/register/payInfo/purchasedProduct',
		data : {"id":2,"userId":1,"uid":22098,"orderNumber":"201602141413242575"},
		success : function(data){
			if (data.success == 'succeed') {
				 appId = data.data.appId;
				 timeStamp = data.data.paytimestamp;
				 nonceStr = data.data.paynonceStr;
				 Package = data.data.paypackage;
				 signType = data.data.paysignType;
				 paySign = data.data.paySign;
				 orderNumber=data.data.orderNumber;
				$("#hemtl").html("orderNumber="+orderNumber+",appId="+appId+",timeStamp="+timeStamp
						+",nonceStr = "+nonceStr +",Package=" +Package
						+",signType = " + signType +",paySign = " + paySign);
			} 
		}
	});
	
});

function onBridgeReady() {
    if(appId != null || appId != ''){
    	WeixinJSBridge.invoke('getBrandWCPayRequest', {
    	      "appId" : appId,//"wx2421b1c4370ec43b", //公众号名称，由商户传入   
    	      "timeStamp" : timeStamp,//"1395712654", //时间戳，自1970年以来的秒数   
    	      "nonceStr" : nonceStr,//"e61463f8efa94090b1f366cccfbbb444", //随机串   
    	      "package" : Package,//"prepay_id=u802345jgfjsdfgsdg888",
    	      "signType" : signType,//"MD5", //微信签名方式:   
    	      "paySign" : paySign,//"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
    	    }, function(res) { // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回  ok，但并不保证它绝对可靠。  
    	      //alert(res.err_msg);
    	      if (res.err_msg == "get_brand_wcpay_request:ok") {
    	    	  callbak("ok");
    	        alert("支付成功");
    	      }
    	      if (res.err_msg == "get_brand_wcpay_request:cancel") {
    	    	  callbak("cancel");
    	        alert("交易取消");
    	      }
    	      if (res.err_msg == "get_brand_wcpay_request:fail") {
    	    	  callbak("fail");
    	        alert("支付失败");
    	      }
    	    });
    }else{
    	alert("接口失败")
    }
    
  }
  
  function callbak(state){
	  state ="ok";
	  $.ajax({
			type : "post",
			dataType : 'json',
			async: false,
			url : '/wcfsy-wechat/register/payInfo/paymentCallBak',
			data : {"productId":1,"uid":22098,"orderNumber":orderNumber,"state":state},
			success : function(data){
				if (data.success == 'succeed') {
					alert("更新成功")
				}else{
					alert(data.message);
				}
			}
		});
  }
  
  function callPay() {
    if (typeof WeixinJSBridge == "undefined") {
      if (document.addEventListener) {
        document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
            false);
      } else if (document.attachEvent) {
        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
      }
    } else {
      onBridgeReady();
    }
  }
  
  
  
</script>
</head>
<body>
	<input type="button" name="" style="width: 100rm;height: 35rm;" value="支付" onclick="onBridgeReady();"/>
	<div id ="hemtl"></div>
</body>
</html>
